<template>
  <div>
    <div class="posInput">
      <el-input
          size="small"
          class="addPosInput"
          element-loading-text="正在加载..."
          element-loading-spinner="el-icon-loading"
          element-loading-background="rgba(0, 0, 0, 0.8)"
          placeholder="添加职位..."
          prefix-icon="el-icon-plus"
          @keydown.enter.native="addPosition"
          v-model="pos.name">
      </el-input>
      <el-button type="primary" size="small" @click="addPosition">添加</el-button>
    </div>
    <div>
      <el-table
          :data="positions"
          border
          stripe
          @selection-change="handleSelectionChange"
          style="width: 100%">
        <el-table-column
            type="selection"
            width="55">
        </el-table-column>
        <el-table-column
            prop="id"
            label="编号"
            width="180">
        </el-table-column>
        <el-table-column
            prop="name"
            label="职称名称"
            width="180">
        </el-table-column>
        <el-table-column
            prop="createDate"
            label="创建时间">
        </el-table-column>
        <el-table-column
            label="是否启用">
          <template slot-scope="scope">
            <el-tag size="small" type="success" v-if="scope.row.enabled">已启用</el-tag>
            <el-tag size="small" type="danger" v-else>未启用</el-tag>
          </template>
        </el-table-column>
        <el-table-column>
          <template slot-scope="scope">
            <el-button
                size="mini"
                @click="showEditView(scope.$index, scope.row)">编辑
            </el-button>
            <el-button
                size="mini"
                type="danger"
                @click="handleDelete(scope.$index, scope.row)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-button @click="deleteMany" type="danger" size="small" style="margin-top: 8px"
                 :disabled="selectPositions.length==0">批量删除
      </el-button>
    </div>
    <div>
      <el-dialog title="修改职位" :visible.sync="isShowDialog">
        <el-form :model="updatePos">
          <el-form-item label="职位名称" :label-width="formLabelWidth">
            <el-input v-model="updatePos.name" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="是否启用" :label-width="formLabelWidth">
            <el-switch
                v-model="updatePos.enabled"
                active-text="启用"
                inactive-text="禁用">
            </el-switch>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="isShowDialog = false">取 消</el-button>
          <el-button type="primary" @click="editPosition(pos.id)">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import {getRequest, putRequest} from "@/utils/api";

export default {
  name: "PosManager",
  data() {
    return {
      pos: {
        name: ""
      },
      positions: [],
      isShowDialog: false,
      formLabelWidth: '80px',
      updatePos: {
        name:"",
        enabled:false
      },
      selectPositions: []
    }
  },
  mounted() {
    this.initPositions();
  },
  methods: {
    handleSelectionChange(selectPositions) {
      this.selectPositions = selectPositions;
    },
    initPositions() {
      getRequest("/system/basic/pos/").then((resp) => {
        this.positions = resp;
      })
    },
    showEditView(index, data) {
      this.isShowDialog = true;
      //克隆该对象
      Object.assign(this.updatePos, data);
    },
    editPosition(id) {
      putRequest("/system/basic/pos/", this.updatePos).then(success => {
        if (success) {
          this.initPositions();
          this.isShowDialog = false;
          this.updatePos = {}
        }
      })
    },
    addPosition() {
      if (this.pos.name) {
        this.postRequest("/system/basic/pos/", this.pos).then(resp => {
          if (resp) {
            this.initPositions();
            this.pos.name = '';
          }
        })
      } else {
        this.$message.error('职位名称不可以为空');
      }
    },
    handleDelete(index, data) {
      this.$confirm('此操作将永久删除【' + data.name + '】职位, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.deleteRequest("/system/basic/pos/" + data.id).then(resp => {
          if (resp) {
            this.initPositions();
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    deleteMany() {
      this.$confirm('此操作将永久删除【' + this.selectPositions.length + '】条记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let ids = '?';
        this.selectPositions.forEach(item => {
          ids += 'ids=' + item.id + '&';
        })
        this.deleteRequest("/system/basic/pos/" + ids).then(resp => {
          if (resp) {
            this.initPositions();
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    }
  }
}
</script>

<style scoped>
.posInput {
  margin-bottom: 10px;
}

.addPosInput {
  width: 300px;
  margin-right: 8px
}
</style>